package com.sd.web.im;

import com.sd.common.beans.*;
import com.sd.sms.cmpp.HttpSmsUtils;
import com.sd.sms.cmpp.SmsUtils;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/sms")
public class SmsController {

    private static final Logger logger = LoggerFactory.getLogger(SmsController.class);

    @ApiOperation(value = "cmpp短信发送模块", notes="cmpp短信发送模块")
    @PostMapping("/cmpp/sendmsg")
    public String sendSmsByCmpp(@RequestBody SmsInfo smsInfo){

        logger.info("------请求的参数是："+smsInfo.toString());

        int result = 0;

        if(null == smsInfo || null == smsInfo.getMessage() || null == smsInfo.getPhoneNum()){
            return "短信信息或手机号码不能为空";
        }
        try {
            SmsUtils.connectISMG();
            result = SmsUtils.sendShortMsg(smsInfo.getMessage(),smsInfo.getPhoneNum());
            logger.info("------请求cmpp发送短信的结果result："+result);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return String.valueOf(result);
    }

    @ApiOperation(value = "http接口短信发送模块", notes="http接口短信发送模块")
    @PostMapping("/http/sendmsg")
    public String sendSmsByHttp(@RequestBody List<HttpSmsInfo> httpSmsInfoList){

        if(null == httpSmsInfoList || httpSmsInfoList.size() < 1){
            return "需要发送的短信不能为空";
        }

        logger.info("------请求的参数是："+httpSmsInfoList.toString());

        String result = HttpSmsUtils.httpSendSms(httpSmsInfoList);

        return result;
    }

    @ApiOperation(value = "http接口短信获取余额", notes="http接口短信获取余额")
    @GetMapping("/http/getbalance")
    public String getbalance(){

        String result = HttpSmsUtils.getbalance();

        logger.info("------获取余额信息："+result);

        return result;
    }

}
